package com.caigou.modules.article.adpter

import android.app.Activity
import android.graphics.Color
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.caigou.comm.base.bean.CommentItemBean
import com.caigou.comm.base.ext.GlideDisplay
import com.caigou.comm.base.ext.levelImage
import com.caigou.comm.base.ext.showImgsPreview
import com.caigou.comm.base.utils.ImageLoader
import com.caigou.modules.article.R
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lwkandroid.widget.ngv.DefaultNgvAdapter
import com.lwkandroid.widget.ngv.NgvChildImageView
import com.lwkandroid.widget.ngv.NineGridView

/**
 * @author #Suyghur.
 * Created on 2021/11/20
 */

class ArticleCommentItemAdapter(private val activity: Activity, dataset: MutableList<CommentItemBean>) :
    BaseQuickAdapter<CommentItemBean, BaseViewHolder>(R.layout.item_comment_list, dataset), LoadMoreModule {
    init {
        addChildClickViewIds(R.id.item_comment_iv_avatar)
        addChildClickViewIds(R.id.item_comment_ll_content)
        addChildClickViewIds(R.id.item_comment_iv_like)
        addChildClickViewIds(R.id.item_comment_btn_reply)
        addChildClickViewIds(R.id.item_comment_iv_report)
    }

    override fun convert(holder: BaseViewHolder, item: CommentItemBean) {
        // 最顶层评论
        if (TextUtils.isEmpty(item.replyUserInfoVo.headImg)) {
            holder.setImageResource(R.id.item_comment_iv_avatar, R.drawable.img_comm_avatar)
        } else {
            ImageLoader.loadCircle(context, item.replyUserInfoVo.headImg, holder.getView(R.id.item_comment_iv_avatar))
        }
        holder.setText(R.id.item_comment_tv_floor, "${item.floor}楼")
        holder.setText(R.id.item_comment_tv_nickname, item.replyUserInfoVo.nickName)
        holder.setText(R.id.item_comment_tv_time, item.createTime)
        holder.setText(R.id.item_comment_tv_content, item.content)
        holder.setText(R.id.item_comment_tv_like, item.nicePoint.toString())
        holder.setImageResource(R.id.item_comment_iv_lv, levelImage(item.replyUserInfoVo.level))

        if (item.like) {
            holder.setImageResource(R.id.item_comment_iv_like, R.drawable.img_article_like)
            holder.getView<TextView>(R.id.item_comment_tv_like).setTextColor(Color.parseColor("#FE6880"))
        } else {
            holder.setImageResource(R.id.item_comment_iv_like, R.drawable.img_article_unlike)
            holder.getView<TextView>(R.id.item_comment_tv_like).setTextColor(Color.parseColor("#505050"))
        }

        holder.getView<NineGridView>(R.id.item_comment_ngv_display).run {
            if (item.contentImgs.size > 0) {
                visibility = View.VISIBLE
                setSingleImageSize(TypedValue.COMPLEX_UNIT_DIP, 150, 200)
                val ngvAdapter = DefaultNgvAdapter(100, GlideDisplay())
                ngvAdapter.dataList = item.contentImgs
                ngvAdapter.setOnChildClickListener(object : DefaultNgvAdapter.OnChildClickedListener<String> {
                    override fun onPlusImageClicked(plusImageView: ImageView?, dValueToLimited: Int) {

                    }

                    override fun onContentImageClicked(position: Int, data: String?, childImageView: NgvChildImageView?) {
                        data?.apply {
                            showImgsPreview(activity, item.contentImgs, item.contentImgs.indexOf(this))
                        }
                    }

                    override fun onImageDeleted(position: Int, data: String?) {
                    }

                })
                adapter = ngvAdapter
            } else {
                visibility = View.GONE
            }
        }

        if (item.children.size <= 0) {
            holder.getView<LinearLayout>(R.id.item_comment_ll_reply).visibility = View.GONE
        } else {
            holder.getView<LinearLayout>(R.id.item_comment_ll_reply).visibility = View.VISIBLE
            val childItem = item.children[0]
            if (TextUtils.isEmpty(childItem.replyUserInfoVo.headImg)) {
                holder.setImageResource(R.id.comment_reply_iv_avatar1, R.drawable.img_comm_avatar)
            } else {
                ImageLoader.loadCircle(context, childItem.replyUserInfoVo.headImg, holder.getView(R.id.comment_reply_iv_avatar1))
            }
            holder.setText(R.id.comment_reply_tv_nickname1, childItem.replyUserInfoVo.nickName)
            holder.setText(R.id.comment_reply_tv_content, childItem.content)
            if (childItem.replyUserInfoVo.userId == childItem.receiverUserInfoVo.userId) {
                // 自己回复自己
                holder.getView<TextView>(R.id.comment_reply_tv_desc).visibility = View.GONE
                holder.getView<ImageView>(R.id.comment_reply_iv_avatar2).visibility = View.GONE
                holder.getView<TextView>(R.id.comment_reply_tv_nickname2).visibility = View.GONE
            } else {
                // 别人回复自己
                if (TextUtils.isEmpty(childItem.receiverUserInfoVo.headImg)) {
                    holder.setImageResource(R.id.comment_reply_iv_avatar2, R.drawable.img_comm_avatar)
                } else {
                    ImageLoader.loadCircle(context, childItem.receiverUserInfoVo.headImg, holder.getView(R.id.comment_reply_iv_avatar2))
                }
                holder.setText(R.id.comment_reply_tv_nickname2, childItem.receiverUserInfoVo.nickName)
            }
        }
    }
}